class Solution
{
public:
    bool containsNearbyAlmostDuplicate(vector<int> &nums, int indexDiff, int valueDiff)
    {
        set<int> leftNums;
        int n = nums.size();
        for (int i = 0; i < n; ++i)
        {
            auto firstWithinLowerBoundIt = leftNums.lower_bound(max(nums[i], INT_MIN + valueDiff) - valueDiff);
            if (firstWithinLowerBoundIt != leftNums.end() && *firstWithinLowerBoundIt <= min(nums[i], INT_MAX - valueDiff) + valueDiff)
            {
                return true;
            }
            leftNums.insert(nums[i]);
            if (i >= indexDiff)
            {
                leftNums.erase(nums[i - indexDiff]);
            }
        }
        return false;
    }
};